<?php

namespace Mp\Controller;

use Common\Controller\MyController;
use Common\Service\UploadService;
use Common\Service\SysFuncService;
use Common\Service\SysFuncOperationService;
use Common\Service\SysRolePermissionService;

class MpController extends MyController {

  const SYS_TYPE = 3 ;    //系统类型
  var $auth;               //用户信息      
  var $session_name;      //session名称
  var $base_path ; 
  var $module ;            //如Backend
  var $controller ;
  var $action ;
  var $func_url ;
  var $menu_id ;
  var $mer_id ;
  var $opt_data ;         //可用操作
  
  var $action_label = array(
    'read'   => '查看',
    'create' => '新增',
    'update' => '编辑',
    'delete' => '删除',
  );

  public function __construct() {
    parent::__construct();
    //检查是否登录
    $this->session_name = C('SESSION_NAME');
    $this->auth = session($this->session_name);
    $this->module = MODULE_NAME ;
    $this->module_path = strtolower( MODULE_NAME ) ;
    $this->controller = CONTROLLER_NAME ; 
    $this->base_path = MODULE_NAME . '/' . CONTROLLER_NAME . '/';
    $this->mer_id = $this->auth['mer_data']['id'];
    
    //检查是否登录
    if (empty($this->auth)) {
      redirect( U( $this->module . '/auth/login') );
      exit();
    }

    //检查是否有权限
    $this->func_url = MODULE_NAME . '/' . CONTROLLER_NAME . '/index' ;
    $this->menu_id = 'menu_' . str_replace('/', '_', $this->func_url) ;
    
    //换成操作别名
    $SysFuncOperation = SysFuncOperationService::instance();
    $opt_name = $SysFuncOperation->get_opt_name_by_action_name(ACTION_NAME);

    //验证权限
    $SysRolePermission = SysRolePermissionService::instance();
    $this->opt_data = $SysRolePermission->check_by_role_opt_func($this->auth['role_id'], $opt_name, $this->func_url ) ;
    
    //如果无权限
    if ( !$this->opt_data ) {
      $this->_access_denied();
    }
  }
  
  /**
   * 空操作
   */
  function _empty() {
    $this->display('Public:404');
  }
  
  /**
   * 访问拒绝
   */
  function _access_denied() {
    if ( is_ajax_request() ) {
      //如果是AJAX请求
      $this->ajaxReturn(ajax_arr('拒绝访问'));
    } else {
      //如果普通访问
      gameover('拒绝访问');
    }
  }

  /**
   * 获取菜单
   * @return type
   */
  function _get_menu() {
    $menu_session = C('SESSION_MENU_NAME');
    $data = session( $menu_session );
    if ( empty( $data ) ) {
      $SysFunc = SysFuncService::instance();
      $data = $SysFunc->get_menu_by_roles( $this->auth['role_id'] , self::SYS_TYPE );
      session( $menu_session , $data );
    }
    return $data ;
  }
  
  /**
   * 获取页面数据
   * @return type
   */
  function _init_data() {
    return array(
      'site_name' => C('SITE_NAME'),
      'username' => $this->auth ['username'] ,
      'mer_name' => $this->auth['mer_data']['name'] ,
      'mer_id' => $this->auth['mer_data']['id'],
      'menu_data' => $this->_get_menu() ,
      'opt_data' => $this->opt_data ,
      'base_url' => site_url ( '/Backend/index' ),
      'home_url' => U( $this->module . '/index/index'),
      'module' => $this->module ,
      'module_title' => '',
    );
  }

  /**
   * 按布局显示
   * @param type $data
   * @param type $view
   */
  function display_with_layout($data, $view) {
    $this->assign($data);
    $layout_data = array(
      'menu' => $this->fetch('Public:menu'),
      'body' => $this->fetch($view),
    );
    $this->assign($layout_data);
    $this->display('Public:layout');
  }

  /**
   * 上传路径
   * @param type $type
   */
  function upload($type = 'icon') {
    $upload = UploadService::instance();
    $ret = $upload->do_upload($type);
    $this->ajaxReturn($ret);
  }
  
  /**
   * 删除图标
   * @return type
   */
  function delete_icon() {
    $icon = I('post.icon');
    if ( !strstr($icon, 'Public/upload/icon') ) {
      return ;
    }
    delete_file($icon);
  }

}
